package com.ksd.modules.special.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.modules.special.entity.AdminSpecial;
import com.ksd.modules.special.po.AdminSpecialSavePO;
import com.ksd.modules.special.po.AdminSpecialSearchPO;
import com.ksd.modules.special.service.IAdminSpecialService;
import com.ksd.modules.special.vo.AdminSpecialVo;
import com.ksd.redis.util.RedisUtil_Back;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class AdminSpecialController extends BaseController {
    
    @Autowired
    private IAdminSpecialService adminSpecialService;
    @Autowired
    private RedisUtil_Back redisUtilBack;
    private static final String CACHE_CHANNEL_KEY1 = "KSD:SYSTEM:SPECIAL:GetUserStudyDetailByPath:";
    private static final String CACHE_CHANNEL_KEY2 = "KSD:SYSTEM:SPECIAL:GetSpecialByPath:";
    private static final String CACHE_CHANNEL_KEY3 = "KSD:SYSTEM:SPECIAL:GetUserStudyDetailById:";
    private static final String CACHE_CHANNEL_KEY4 = "KSD:SYSTEM:SPECIAL:GetSpecialByID:";
    /**
     * 【专题】- 列表
     * @return
     */
    @PostMapping("/special/list")
    public R<Page<AdminSpecialVo>> findAdminSpecials(@Validated @RequestBody AdminSpecialSearchPO adminSpecialSearchPO){
        return R.data(adminSpecialService.findAdminSpecials(adminSpecialSearchPO));
    }

    /**
     * 【专题】- 回收站
     * @return
     */
    @PostMapping("/special/recyle")
    public R<Page<AdminSpecialVo>> findAdminSpecialsRecyle(@Validated @RequestBody AdminSpecialSearchPO adminSpecialSearchPO){
        return R.data(adminSpecialService.findAdminSpecialsRecyle(adminSpecialSearchPO));
    }

    /**
     * 【专题】 - 保存和更新
     * @param adminSpecialSavePO
     * @return
     */
    @PostMapping("/special/saveupdate")
    @ApiLog("【专题】 - 保存和更新")
    @RepeatSubmit
    public R<AdminSpecialVo> saveUpdateAdminSpecial(@Validated @RequestBody AdminSpecialSavePO adminSpecialSavePO){
        return R.data(adminSpecialService.saveUpdateAdminSpecial(adminSpecialSavePO));
    }

    /**
     * 【专题】 - 根据id查询明细
     * @param id
     * @return
     */
    @PostMapping("/special/get/{id}")
    public R<AdminSpecialVo> getAdminSpecialById(@PathVariable("id")Long id){
        return R.data(adminSpecialService.getAdminSpecialById(id));
    }

    /**
     * 【专题】 - 根据id删除专题
     * @param id
     * @return
     */
    @PostMapping("/special/del/{id}")
    @ApiLog("【专题】 - 根据id删除专题 ")
    public R<Boolean> deleteAdminSpecialById(@PathVariable("id")Long id){
        return R.data(adminSpecialService.deleteAdminSpecialById(id));
    }

    /**
     * 【专题】- 根据id恢复删除
     *
     * @param id
     * @return
     */
    @PostMapping("/special/recover/{id}")
    @ApiLog("【专题】- 根据id恢复删除")
    @RepeatSubmit
    public R<Boolean> recoverByIdAdminSpecial(@PathVariable("id") Long id) {
        return R.data(adminSpecialService.recoverByIdAdminSpecial(id));
    }

    /**
     * 【专题】- 批量逻辑删除
     *
     * @return
     */
    @PostMapping("/special/batch/dels")
    @ApiLog("【专题】- 批量逻辑删除")
    @RepeatSubmit
    public R<Boolean> bacthDeletesAdminSpecial(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminSpecialService.bacthDeletesAdminSpecial(batchIdsPO));
    }


    /**
     * 【专题】- 批量恢复
     *
     * @return
     */
    @PostMapping("/special/batch/recover")
    @ApiLog("【专题】- 批量恢复")
    @RepeatSubmit
    public R<Boolean> batchRecoversAdminSpecial(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminSpecialService.batchRecoversAdminSpecial(batchIdsPO));
    }
    
    /**
     * 【专题】- 批量物理删除
     *
     * @return
     */
    @PostMapping("/special/batch/remove")
    @ApiLog("【专题】- 批量物理删除")
    @RepeatSubmit
    public R<Boolean> batchRemovesAdminSpecial(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminSpecialService.batchRemovesAdminSpecial(batchIdsPO));
    }

    /**
     * 【专题】- 根据ID恢复删除
     * @param id
     * @return
     */
    @PostMapping("/special/remove/{id}")
    @ApiLog("【专题】- 根据ID恢复删除")
    public R<Boolean> removeAdminSpecialById(@PathVariable("id")Long id){
        return R.data(adminSpecialService.removeAdminSpecialById(id));
    }

    /**
     * 【专题】- 修改状态
     *
     * @return
     */
    @PostMapping("/special/status/{id}/{field}")
    @ApiLog("【专题】- 修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminSpecialService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminSpecialService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【专题】-批量上架
     *
     * @return
     */
    @PostMapping("/special/batch/up")
    @ApiLog("【专题】-批量上架")
    @RepeatSubmit
    public R<Boolean> batchUps(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminSpecialService.batchUps(batchIdsPO));
    }

    /**
     * 【专题】-批量下架
     *
     * @return
     */
    @PostMapping("/special/batch/down")
    @ApiLog("【专题】-批量下架")
    @RepeatSubmit
    public R<Boolean> batchDowns(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminSpecialService.batchDowns(batchIdsPO));
    }

    /**
     * 【专题】-刷新缓存
     *
     * @return
     */
    @PostMapping("/special/refresh/{id}")
    @ApiLog("【专题】-刷新缓存")
    @RepeatSubmit
    public R<Boolean> specialRefresh(@PathVariable("id")Long id) {
        AdminSpecial adminSpecial = adminSpecialService.getById(id);
        if(adminSpecial!=null) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + adminSpecial.getPath() + ":" + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + adminSpecial.getPath() + ":" + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY3 + id + ":" + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY4 + id + ":" + SystemIdThrealLocal.get().getSystemId());
        }
        return R.data(true);
    }

    /**
     * 【专题】 - 复制数据
     * @param opid
     * @return
     */
    @PostMapping("/special/copy/{opid}")
    @ApiLog("【专题】 -复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminSpecialService.copyData(opid));
    }
}
